package com.rchat.pocmini.account;

import android.content.Context;
import android.util.Log;

import com.airtalkee.sdk.AirtalkeeAccount;
import com.airtalkee.sdk.OnAccountListener;
import com.rchat.pocmini.util.AppConfig;

/**
 * Created by ljd on 2017/2/6.
 * <p>
 * 负责所有与用户账户相关的操作方法
 */

public class PocAccount implements PocAccountInterface {

    private static AirtalkeeAccount airtalkeeAccount = AirtalkeeAccount.getInstance();
    private static PocAccount pocAccount;

    public static String getUserId() {
        return userId;
    }

    private void setUserId(String userId) {
        this.userId = userId;
    }

    private static String userId;

    public static String getExpireDate() {
        return expireDate;
    }

    public static void setExpireDate(String expireDate) {
        PocAccount.expireDate = expireDate;
    }

    private static String expireDate;

    private static String pwd;

    public static String getPwd() {
        return pwd;
    }

    public static void setPwd(String p) {
        pwd = p;
    }


    public static String getNewUserName() {
        return newUserName;
    }


    public static boolean isUserNameChanged() {
        return isUserNameChanged;
    }

    public static void setIsUserNameChanged(boolean isUserNameChanged) {
        PocAccount.isUserNameChanged = isUserNameChanged;
    }

    private static boolean isUserNameChanged;

    public static void setNewUserName(String newUserName) {
        PocAccount.newUserName = newUserName;
    }

    private static String newUserName;

    private PocAccount() {

    }

    private static boolean isInited = false;

    /**
     * 获取AirtalkeeAccount实例
     *
     * @return PocAccount
     */
    public static PocAccount getPocAccountInstance(Context context) {
//        if (!isInited){
//            isInited = true;
//            //Airtalkee初始化设置
//            setAirTalkeeConfig(context);
//            //设置SDK Log 开关
//            setAirTalkeeConfigTrace(AppConfig.TRACE_MODE);
//
//            //设置语音文件缓存位置,不能 自行设置，sdk的bug，否则保存不了消息
////            PocAirtalkeeMessage.getPocAirtalkeeMesageInstance().setMessageRecordPath(AppConfig.RECORD_MESSAGE_SAVE_PATH);
//            //开启或关闭实时音频的本地录制
//            PocAirtalkeeSessionManager.getPocAirtalkeeSessionManagerInstance().setSessionLocalRecordEnable(AppConfig.SESSION_LOCAL_RECORD_ENABLE);
//
//        }
        if (pocAccount == null) {
            pocAccount = new PocAccount();
        }
        return pocAccount;
    }

    /**
     * 服务器设置与运行监测
     *
     * @param context 上下文
     */
    private static void setAirTalkeeConfig(Context context) {
        airtalkeeAccount.AirTalkeeConfig(context, AppConfig.SERVER_ADDRESS, AppConfig.SERVER_PORT, true);
    }

    /**
     * * 可通过此接口打开或关闭SDK的Log，如果是文件方式输出，
     * 输出的路径默认为 /SD/AirTalkee/logs/ ，以PocTrace开头的txt文件，
     * 每启动一次SDK，便会生成一个Log文件（SDK文档中说明默认是关闭的（有待验证，最好是手动关闭））
     *
     * @param traceMode LogMode
     */
    private static void setAirTalkeeConfigTrace(int traceMode) {
        airtalkeeAccount.AirTalkeeConfigTrace(traceMode);
    }

    /**
     * 监测账户是否处于已登录状态
     *
     * @return True:已登录  False:未登录
     */
    @Override
    public boolean isAccountRunning() {
        return airtalkeeAccount.isAccountRunning();
    }

    /**
     * 此接口可单独设置“位置上报服务”和“图文上报服务”的地址
     *
     * @param locationAddress 位置上报服务器地址（IP或URL）
     * @param reportAddress   图文上报服务器地址（IP或URL）
     */
    @Override
    public void setAirTalkeeConfigSubServer(String locationAddress, String reportAddress) {
        airtalkeeAccount.AirTalkeeConfigSubServer(locationAddress, reportAddress);
    }

    /**
     * 通过语音平台账号登录，事件返回OnAccountListener.onLogin
     *
     * @param userId 语音平台账户ID
     * @param pwd    密码
     */
    @Override
    public void login(String userId, String pwd) {
        setUserId(userId);
        airtalkeeAccount.Login(userId, pwd);
    }

    /**
     * 登出语音平台，事件返回OnAccountListener.onLogout
     */
    @Override
    public void logout() {

        airtalkeeAccount.Logout();
//        PocAirtalkeeSessionManager.getPocAirtalkeeSessionManagerInstance().setLockedAirSession(null);
    }

    /**
     * 第三方应用通过监听网络状，当收到网络连接断开的事件时，
     * 需要调用此接口，此接口将会自动实现对网络变化的处理
     */
    @Override
    public void netWorkOpen() {
        airtalkeeAccount.NetworkOpen();
    }

    /**
     * 第三方应用通过监听网络状态，当收到网络连接断开的事件时，
     * 需要调用此接口，此接口将会自动实现对网络变化的处理
     */
    @Override
    public void netWorkClose() {
        airtalkeeAccount.NetworkClose();
    }

    /**
     * 第三方应用通过此接口可向服务器上报自定义信息
     *
     * @param json 第三方上报的json报文
     */
    @Override
    public void systemCustomReport(String json) {
        airtalkeeAccount.SystemCustomReport(json);
    }


    /**
     * t
     * 设置AirtalkeeAccount监听器
     *
     * @param listener interface
     */
    @Override
    public void setOnAccountListener(OnAccountListener listener) {
        airtalkeeAccount.setOnAccountListener(listener);
    }


    public static String getUserName() {
        return airtalkeeAccount.getUserName();
    }


}
